package com.tenton.servlet;

import com.tenton.pojo.User;
import com.tenton.service.Impl.UserServiceImpl;
import com.tenton.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Date: 2021/1/19
 * @Author: Tenton
 * @Description: 查询所有用户
 */
@WebServlet(name = "ListUserServlet",urlPatterns = "/listUserServlet")
public class ListUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        UserService userService = new UserServiceImpl();
        //获取所有用户信息
        List<User> users = userService.listUser();
        //把获取到的用户信息链表存入request域中，传递给前端
        request.setAttribute("users",users);
        //请求转发到userManager页面
        request.getRequestDispatcher("/userManager.jsp").forward(request,response);
    }
}
